package org.codeart.singleton;

/**
 * 懒汉式
 * 支持懒加载
 */
public class Singleton02 {

    private static Singleton02 instance;

    private Singleton02() {

    }

    public static Singleton02 getInstance() {
        if (instance == null) {
            instance = new Singleton02();
        }
        return instance;
    }

    public static void main(String[] args) {
        Singleton01 instance1 = Singleton01.getInstance();
        Singleton01 instance2 = Singleton01.getInstance();
        System.out.println(instance1 == instance2);

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                Singleton02 s = getInstance();
                System.out.println(Thread.currentThread().getName() + ": " + s);
            }).start();
        }
    }

}
